package com.puboot.module.blog.controller;

import com.puboot.common.util.Constants;
import com.puboot.common.util.GasDCUtil;
import com.puboot.common.util.PowerGridUtil;
import com.puboot.common.util.ResultUtil;
import com.puboot.module.admin.vo.base.ResponseVo;
import com.puboot.module.blog.model.*;
import com.puboot.module.blog.service.*;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/blog/powergrid/api")
@AllArgsConstructor
public class PowerGridController {

    private final HttpServletRequest httpServletRequest;
    private final PowerGridService powerGridService;
    private final PowerGridRecService powerGridRecService;
    private final PowerGridRepService powerGridRepService;

    @PostMapping("/result")
    @Transactional
    public ResponseVo result(PowerGrid powerGrid) {
        try {
            HttpSession httpSession = httpServletRequest.getSession();
            Customer customer = (Customer) httpSession.getAttribute("customer");
            List<String> recCapacitys = new ArrayList<>();
            if (StringUtils.isNotBlank(powerGrid.getRecCapacitys())) {
                recCapacitys = Arrays.asList(powerGrid.getRecCapacitys().split(","));
            }
            List<String> recRecoverys = new ArrayList<>();
            if (StringUtils.isNotBlank(powerGrid.getRecRecoverys())) {
                recRecoverys = Arrays.asList(powerGrid.getRecRecoverys().split(","));
            }
            List<String> repCapacitys = new ArrayList<>();
            if (StringUtils.isNotBlank(powerGrid.getRepCapacitys())) {
                repCapacitys = Arrays.asList(powerGrid.getRepCapacitys().split(","));
            }
            List<String> repRecoverys = new ArrayList<>();
            if (StringUtils.isNotBlank(powerGrid.getRepRecoverys())) {
                repRecoverys = Arrays.asList(powerGrid.getRepRecoverys().split(","));
            }
            Double esf6 = PowerGridUtil.getEsf6(recCapacitys, recRecoverys,
                    repCapacitys, repRecoverys);
            Double networkLoss = PowerGridUtil.getNetworkLoss(powerGrid.getEfPowerGrid(), powerGrid.getEfSellingElectricity(),
                    powerGrid.getEfSurfTheInternet(), powerGrid.getEfInput(), powerGrid.getEfOut());
            Double result = PowerGridUtil.getE(esf6, networkLoss);
            powerGrid.setCustomerId(customer.getId());
            powerGrid.setCreateTime(new Date());
            powerGrid.setEsf6(GasDCUtil.round(esf6, 2));
            powerGrid.setNetworkLoss(GasDCUtil.round(networkLoss, 2));
            powerGrid.setState(Constants.STATE_INVAILD);
            powerGrid.setResult(GasDCUtil.getThousandth(result));
            powerGridService.save(powerGrid);
            for (int i = 0; i < recCapacitys.size(); i++) {
                PowerGridRec rec = new PowerGridRec();
                rec.setRecCapacity(Double.valueOf(recCapacitys.get(i)));
                rec.setRecRecovery(Double.valueOf(recRecoverys.get(i)));
                rec.setPowerGridId(powerGrid.getId());
                powerGridRecService.save(rec);
            }
            for (int i = 0; i < repCapacitys.size(); i++) {
                PowerGridRep rep = new PowerGridRep();
                rep.setRepCapacity(Double.valueOf(repCapacitys.get(i)));
                rep.setRepRecovery(Double.valueOf(repRecoverys.get(i)));
                rep.setPowerGridId(powerGrid.getId());
                powerGridRepService.save(rep);
            }
            return ResultUtil.success("计算成功", powerGrid);
        } catch (Exception e) {
            log.error("计算失败", e);
            return ResultUtil.error("计算失败");
        }
    }


}
